/**
 * 支付
 */
$(function() {

	// 绑定事件
	$('.recharge-button').bind("click", submitOrder);
	$('.showAcount').bind("click", showUserAcccount);

	// 封装form对象Vo
	function rechargeOrderVo() {
		var rechargeOrder;
		rechargeOrder = utilModule.dealFormFilter('rechargeForm');
		return rechargeOrder;
	}

	// 提交订单 
	function submitOrder() {
		var rechargeOrder = rechargeOrderVo();
		rechargeOrder.userid = USERID;
		rechargeOrder.openid = OPENID;
		var rechargeAmount = $("#rechargeAmount").val();
		var reg = /(^[1-9]([0-9]+)?(\.[0-9]{1,2})?$)|(^(0){1}$)|(^[0-9]\.[0-9]([0-9])?$)/;
		if(rechargeAmount == null || rechargeAmount == 0) {
			mui.alert('充值金额不能为空', '提示');
			return false;
		} else if(!reg.test(rechargeAmount)) {
			mui.alert('金额输入有误', '提示');
			return false;
		} else {
			userAccountModule.submitRechargeOrder(rechargeOrder,
				function(callBack) {
					var payInfo = callBack;
					if(payInfo != null) {
						onBridgeReady(payInfo);
					}

				});
		}
	}
	// 发起微信支付
	function onBridgeReady(data) {
		WeixinJSBridge.invoke('getBrandWCPayRequest', {
			"appId": data.appId, // 公众号名称，由商户传入
			"timeStamp": data.timeStamp, // 时间戳，自1970年以来的秒数
			"nonceStr": data.nonceStr, // 随机串
			"package": data.package,
			"signType": data.signType, // 微信签名方式：
			"paySign": data.paySign
			// 微信签名
		}, function(res) {
			if(res.err_msg == "get_brand_wcpay_request:ok") {
				location.replace("user-balance.html");
			} else if(res.err_msg == "get_brand_wcpay_request:cancel") {
				mui.alert('取消支付！', '提示');
			} else {
				mui.alert('支付失败！', '提示');
			}
		});
	}

	// 查看账户明细
	function initUserInfo() {
		var params = {
			"userid": USERID
		};
		userCenterModule.queryUserInfo(params, function(callBack) {
			var userInfo = callBack;
			if(userInfo.accountbalance != null) {
				var totalMoney = parseInt(userInfo.accountbalance);
				$(".accountBalance").html(totalMoney.toFixed(2));
			}
			$("#totalAcount").val(userInfo.accountbalance);
		});
	}

	function showUserAcccount() {
		location.href = "accountList.html?v=1.1.0";
	}

});